<?php
include_once ('../../file_php.php');
require_once ('../../Utils.php');

$debug = false;
$debugSendMail = false;

ignore_user_abort (); // 关掉浏览器，PHP脚本也可以继续执行.
set_time_limit ( 0 ); // 通过set_time_limit(0)可以让程序无限制的执行下去
$interval = 60 * 5; // 每隔5分钟运行

do {
	if (false == require_once ('enable_schedule.php')) {
		stop ();
	}
	// 这里是你要执行的代码
	$r = require_once ('getweibo.php');
	if (false == $r || true == $debugSendMail) {
		stop ();
	}
	// ---------------------------------------
	$fp = fopen ( Utils::getDataPath () . "test.txt", "w+" );
	fwrite ( $fp, date ( "Y-m-d H:i:s" ) . "\n" );
	fclose ( $fp );
	// ----------------------------------------
	
	if (false == $debug) {
		sleep ( $interval ); // 等待5分钟
	}
} while ( false == $debug );
function stop() {
	// send email to notify me
	$subject = "Oopse,i can't work right now,please fix me.";
	$body = "Info file:" . __FILE__;
	
	// 如果一行大于 70 个字符，请使用 wordwrap()。
	$body = wordwrap ( $body, 70 );
	$headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion ();
	// 发送邮件
	$mailSent = Utils::mail ( "ramonqlee1980@gmail.com", $subject, $body );
	die ( "error to run task"." mail sent:".$mailSent );
}  